package cn.wolfcode.crm.web.controller;

import java.util.List;

import javax.imageio.metadata.IIOMetadataFormat;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.druid.stat.TableStat.Mode;
import com.github.pagehelper.PageInfo;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.query.SystemDictionaryItemQueryObject;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;

@Controller
@RequestMapping("/department")
public class DepartmentController {
	@Autowired
	private IDepartmentService departmentService;
	
	@Autowired
	private ISystemDictionaryItemService systemDictionaryItemService;

	@RequestMapping("/list")
	public String list(@ModelAttribute("qo") QueryObject qo, Model model) {
		PageInfo<Department> pageInfo = departmentService.list(qo);
		
		SystemDictionaryItemQueryObject qo1 = new SystemDictionaryItemQueryObject();
		qo1.setParentId(1L);	//职业
		List<SystemDictionaryItem> jobs = systemDictionaryItemService.listNotPage(qo1);
		qo1.setParentId(2L);	//来源
		List<SystemDictionaryItem> sources = systemDictionaryItemService.listNotPage(qo1);
		qo1.setParentId(3L);	//交流方式
		List<SystemDictionaryItem> types = systemDictionaryItemService.listNotPage(qo1);
		
		model.addAttribute("types", types);
		model.addAttribute("jobs", jobs);
		model.addAttribute("sources", sources);
		model.addAttribute("pageInfo", pageInfo);
		return "department/list";
	}

	@RequestMapping("/saveOrUpdate")
	public String saveOrUpdate(Department entity) {
		departmentService.saveOrUpdate(entity);
		return "redirect:/department/list.do";
	}
	
	@RequestMapping("/input")
	public String input(Long id, Model model) {
		if (id != null) {
			model.addAttribute("entity", departmentService.get(id));
		}
		return "department/input";
	}
	
	@RequestMapping("/delete")
	public String delete(Long id) {
		departmentService.delete(id);
		return "redirect:/department/list.do";
	}
}
